<?php

namespace humhub\modules\api\controllers;

use Yii;
use humhub\modules\space\modules\manage\components\Controller;
use yii\web\HttpException;

use yii\filters\auth\CompositeAuth;
use yii\filters\auth\HttpBearerAuth;

/**
 * 上传/裁剪Space头像, Banner图
 */
class SpaceImageController extends Base2Controller
{

    /**
     * @inheritdoc
     */
    public $hideSidebar = true;

    /**
     * @inheritdoc
     */
    public function init(){
        $_GET['sguid'] = Yii::$app->request->get('s');
        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function behaviors(){
        $behaviors = parent::behaviors();
        $behaviors['authenticator'] = [
            'class' => CompositeAuth::className(),
            'authMethods' => [
                //QueryParamAuth::className(),
                HttpBearerAuth::className(),
            ],
        ];

        $behaviors['acl'] = [
            'class' => \humhub\components\behaviors\AccessControl::className(),
        ];

        return $behaviors;
    }

    /**
     * 上传Space头像
     *
     * POST /api/space-image/upload?s=[guid]
     *
     * get.s 板块Space的guid
     * 
     * post.spacefiles[]  头像文件
     *
     * @return array url:上传的图片访问URL
     */
    public function actionUpload(){
        $this->adminOnly();
        $model = new \humhub\models\forms\UploadProfileImage();

        $json = array();

        $files = \yii\web\UploadedFile::getInstancesByName('spacefiles');
        if( ! isset($files[0]) ){
            throw new \yii\web\BadRequestHttpException('没有上传文件');
        }
        $file = $files[0];
        $model->image = $file;

        if ($model->validate()) {
            $json['error'] = false;
            $profileImage = new \humhub\libs\ProfileImage($this->getSpace()->guid);
            $profileImage->setNew($model->image);
            //$json['size'] = $model->image->size;
            $json['url'] = $profileImage->getUrl();
        } else {
            //$model->getErrors()
            throw new \yii\web\BadRequestHttpException('上传文件失败');
        }
        return $json;
    }

    /**
     * 裁剪当前Space头像
     *
     * POST /api/space-image/crop?s=[guid]
     *
     * get.s 板块Space的guid
     *
     * post.CropProfileImage[cropH]  高
     *
     * post.CropProfileImage[cropW]  宽
     *
     * post.CropProfileImage[cropX]  坐标X
     *
     * post.CropProfileImage[cropY]  坐标Y
     *
     * @return boolean
     */
    public function actionCrop(){
        $this->adminOnly();
        $space = $this->getSpace();
        $model = new \humhub\models\forms\CropProfileImage();
        $profileImage = new \humhub\libs\ProfileImage($space->guid);

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $profileImage->cropOriginal($model->cropX, $model->cropY, $model->cropH, $model->cropW);
            return true;
        }
        throw new \yii\web\BadRequestHttpException('保存失败');
    }

    /**
     * 上传Space板块Banner图
     *
     * POST /api/space-image/banner-upload?s=[guid]
     *
     * get.s 板块Space的guid
     *
     * post.bannerfiles[]  Banner图文件
     *
     * @return array url:上传的图片访问URL
     */
    public function actionBannerUpload(){
        $this->adminOnly();
        $model = new \humhub\models\forms\UploadProfileImage();
        $json = array();

        $files = \yii\web\UploadedFile::getInstancesByName('bannerfiles');
        if( ! isset($files[0]) ){
            throw new \yii\web\BadRequestHttpException('没有上传文件');
        }
        $file = $files[0];
        $model->image = $file;

        if ($model->validate()) {
            $profileImage = new \humhub\libs\ProfileBannerImage($this->getSpace()->guid);
            $profileImage->setNew($model->image);

            $json['url'] = $profileImage->getUrl();
        } else {
            //$model->getErrors()
            throw new \yii\web\BadRequestHttpException('上传文件失败');
        }
        return $json;
    }

    /**
     * 裁剪当前Space的Banner图
     *
     * POST /api/space-image/crop-banner?s=[guid]
     *
     * get.s 板块Space的guid
     *
     * post.CropProfileImage[cropH]  高
     *
     * post.CropProfileImage[cropW]  宽
     *
     * post.CropProfileImage[cropX]  坐标X
     *
     * post.CropProfileImage[cropY]  坐标Y
     *
     * @return boolean
     */
    public function actionCropBanner(){
        $this->adminOnly();
        $space = $this->getSpace();
        $model = new \humhub\models\forms\CropProfileImage();
        $profileImage = new \humhub\libs\ProfileBannerImage($space->guid);

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $profileImage->cropOriginal($model->cropX, $model->cropY, $model->cropH, $model->cropW);
            return true;
        }
        throw new \yii\web\BadRequestHttpException('保存失败');
    }

    /**
     * @inheritdoc
     */
    public function adminOnly()
    {
        if (!$this->getSpace()->isAdmin())
            throw new HttpException(403, 'Access denied - Space Administrator only!');
    }

    /**
     * @inheritdoc
     */
    public function ownerOnly()
    {
        $workspace = $this->getSpace();

        if (!$workspace->isSpaceOwner() && !Yii::$app->user->isAdmin())
            throw new HttpException(403, 'Access denied - Space Owner only!');
    }
}
?>
